﻿using Hxj.Data;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Client.DAL
{
    public class DALFinancial : Base.BaseService
    {

        #region 获取 考试缴费信息
        public DataTable GetExamineePayFee(string strExamPlaceID, string strIdentityCard)
        {
            string sql = "select a.*,b.NMT_SignUpID,b.ExamProvince,b.Name,b.IdentityCard from View_PayFeeList as a left join TB_SignUpStudents as b on a.SignUpID=b.SignUpID where a.ExamPlaceNO='{0}' and b.IdentityCard='{1}'";
            sql = string.Format(sql, strExamPlaceID, strIdentityCard);
            return DbHelper.GetDatabase().FromSql(sql).ToDataTable();
        }
        #endregion

        #region  保存 财务订单付款
        public bool SaveSignUpNetPayFee(TB_SignUpStudentsNetPayFee_Head model, Hxj.Data.DbTrans trans)
        {
            if (!model.IsNewModel)
            {
                return DbHelper.GetDatabase().Update<TB_SignUpStudentsNetPayFee_Head>(model, trans) == 1 ? true : false;
            }
            else
            {
                return DbHelper.GetDatabase().Insert<TB_SignUpStudentsNetPayFee_Head>(model, trans) == 1 ? true : false;
            }
        }
        #endregion

        #region 保存 财务订单明细
        public bool SaveSignUpNetPayFeeDetail(List<TB_SignUpStudentsNetPayFee_Detail> PayFee_Detaillist, Hxj.Data.DbTrans trans)
        {
            bool bo = false;
            if (PayFee_Detaillist != null)
            {
                if (PayFee_Detaillist.Count > 0)
                {
                    foreach (TB_SignUpStudentsNetPayFee_Detail model in PayFee_Detaillist)
                    {
                        if (!model.IsNewModel)
                        {
                            bo = DbHelper.GetDatabase().Update<TB_SignUpStudentsNetPayFee_Detail>(model, trans) == 1 ? true : false;
                        }
                        else
                        {
                            bo = DbHelper.GetDatabase().Insert<TB_SignUpStudentsNetPayFee_Detail>(model, trans) == 1 ? true : false;
                        }

                    }
                }
            }
            return bo;
        }
        #endregion

        #region 保存 缴费确认的信息 （现场报名 更新“是否缴费"字段状态）
        public bool UpdateSpecialityInfoState(List<TB_Exam_SignUpStudentsSpeciality> StudentSpecialityInfo, Hxj.Data.DbTrans trans)
        {
            bool bo = false;
            if (StudentSpecialityInfo != null)
            {
                if (StudentSpecialityInfo.Count > 0)
                {
                    foreach (TB_Exam_SignUpStudentsSpeciality model in StudentSpecialityInfo)
                    {
                        Dictionary<Field, object> dic = new Dictionary<Field, object>();
                        if (model.ExamLevel.ToSafeInt() == 2)
                        {
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Second, model.IsFee_Second);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.OrderNo_Second, model.OrderNo_Second);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Second, model.AppointmentGroup_Second);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Second, model.AppointmentGroupTime_Second);
                        }
                        else if (model.ExamLevel.ToSafeInt() == 3)
                        {
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Third, model.IsFee_Third);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.OrderNo_Third, model.OrderNo_Third);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Third, model.AppointmentGroup_Third);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Third, model.AppointmentGroupTime_Third);
                        }
                        else if (model.ExamLevel.ToSafeInt() == 4)
                        {
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Fourth, model.IsFee_Fourth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.OrderNo_Fourth, model.OrderNo_Fourth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Fourth, model.AppointmentGroup_Fourth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Fourth, model.AppointmentGroupTime_Fourth);
                        }
                        else if (model.ExamLevel.ToSafeInt() == 5)
                        {
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Fifth, model.IsFee_Fifth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.OrderNo_Fifth, model.OrderNo_Fifth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Fifth, model.AppointmentGroup_Fifth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Fifth, model.AppointmentGroupTime_Fifth);
                        }
                        else if (model.ExamLevel.ToSafeInt() == 6)
                        {
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.IsFee_Sixth, model.IsFee_Sixth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.OrderNo_Sixth, model.OrderNo_Sixth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_Sixth, model.AppointmentGroup_Sixth);
                            dic.Add(TB_Exam_SignUpStudentsSpeciality._.AppointmentGroupTime_Sixth, model.AppointmentGroupTime_Sixth);
                        }
                        if (dic.Count > 0)
                        {
                            bo = DbHelper.GetDatabase().Update<TB_Exam_SignUpStudentsSpeciality>(dic, TB_Exam_SignUpStudentsSpeciality._.SignUpStudentsSpecialityID == model.SignUpStudentsSpecialityID) == 1 ? true : false;
                        }
                    }
                }
            }
            return bo;
        }
        #endregion

        #region 查询 考试缴费信息
        public DataTable SearchPayFeeInfo(WhereClip clip)
        {
            return DbHelper.GetDatabase().From<TB_SignUpStudentsNetPayFee_Detail>() //选择表
                  .LeftJoin<TB_SignUpStudentsNetPayFee_Head>(TB_SignUpStudentsNetPayFee_Detail._.OrdNO == TB_SignUpStudentsNetPayFee_Head._.OrdNO)
                  .LeftJoin<TB_Sys_SpecialityKinds>(TB_Sys_SpecialityKinds._.SpecialityKindsNO == TB_SignUpStudentsNetPayFee_Detail._.SpecialityKindsNO)
                  .LeftJoin<TB_Sys_ExamPlace>(TB_Sys_ExamPlace._.ExamPlaceNO == TB_SignUpStudentsNetPayFee_Detail._.ExamPlaceNO)
                  .LeftJoin<TB_Dic_ExamLevel>(TB_Dic_ExamLevel._.DicNO == TB_SignUpStudentsNetPayFee_Detail._.ExamLevel)
                  .LeftJoin<TB_SignUpStudents>(TB_SignUpStudents._.SignUpID == TB_SignUpStudentsNetPayFee_Head._.SignUpID && TB_SignUpStudentsNetPayFee_Head._.OrdNO == TB_SignUpStudentsNetPayFee_Detail._.OrdNO)
                  .Select(TB_SignUpStudentsNetPayFee_Detail._.All, TB_SignUpStudents._.Name, TB_SignUpStudents._.IdentityCard,
                  TB_Sys_SpecialityKinds._.SpecialityKindsName, TB_Dic_ExamLevel._.DicName, TB_SignUpStudentsNetPayFee_Head._.PaySource, TB_SignUpStudentsNetPayFee_Head._.IsFee)  //设置选择的字段数目 
                  .Where(clip)
                  .ToDataTable();   //返回List

        }
        #endregion


        #region 获取 考生报考专业,考试级别信息
        public DataTable GetExameeSpeciality_ExamLevelBySignUpID(WhereClip clip)
        {
            return DbHelper.GetDatabase().From<TB_Exam_SignUpStudentsSpeciality>() //选择表
                                       .LeftJoin<TB_Sys_SpecialityKinds>(TB_Sys_SpecialityKinds._.SpecialityKindsNO == TB_Exam_SignUpStudentsSpeciality._.SpecialityKindsNO)
                                       .LeftJoin<TB_Dic_ExamLevel>(TB_Dic_ExamLevel._.DicNO == TB_Exam_SignUpStudentsSpeciality._.ExamLevel)
                                       .LeftJoin<TB_Sys_ExamPlace>(TB_Sys_ExamPlace._.ExamPlaceNO == TB_Exam_SignUpStudentsSpeciality._.ExamPlaceNO)
                                        .LeftJoin<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == TB_Exam_SignUpStudentsSpeciality._.AppointmentGroup_First)
                                       .Select(TB_Exam_SignUpStudentsSpeciality._.All, TB_Sys_SpecialityKinds._.SpecialityKindsName, TB_Dic_ExamLevel._.DicName,
                                              TB_Sys_ExamPlace._.ExamPlaceName, TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupName) //设置选择的字段数目 
                                       .Where(clip)
                                       .OrderBy(new OrderByClip(TB_Exam_SignUpStudentsSpeciality._.ExamLevel, OrderByOperater.ASC))
                                       .ToDataTable();   //返回List
        }
        #endregion
    }
}
